<?php

    //=============================
    //填写基本变量等初始设置
    //=============================

    $allowedOrigins = [
        'http://t.oa.com',
        'http://oa.enpingshi.com'
    ];

    $origin = isset($_SERVER['HTTP_ORIGIN']) ? $_SERVER['HTTP_ORIGIN'] : '';

    if (in_array($origin, $allowedOrigins)) {
        header("Access-Control-Allow-Origin: $origin");
    }

    header("Access-Control-Allow-Methods: POST");
    header("Access-Control-Allow-Headers: Content-Type");


    //104932918958782 = wedding invitation
    $pageId          = $_REQUEST ['page_id'];
    $pageAccessToken = $_REQUEST ['accesstoken'];
    $message         = $_REQUEST['content'];
    $media           = $_REQUEST['media'];
    $file_number     = $_REQUEST['file_number'];

    $pageId="104932918958782";
    $pageAccessToken = "EAAZAURpDrFlUBO0xjmKZCZBzr6qZCj1TSZApDEyuyLyuxscnT2RrluF60Xgw0iosiPuk04QlZBpZCPNY74uUsbshfG1ZAOsNJYTdZBLMkZBa4HJYyZAZCHJ6jSX5jYZAE1kbGIk6Mg3XIZA399ZCajhZCXZBpNu3gzWmuPtZCoVs4pVR5bceGPklmouZAefZB7U75q8lSFtr2skDJZBOk90ZBzw6PL2H3zyuCKIJEZD";








    //=============================
    //接口要求，需要先初始化一下，获取视频上传地址
    //=============================

    //curl -X POST "https://graph.facebook.com/v22.0/Your_page_id/video_reels" \
    //-H "Content-Type: application/json" \
    //-d '{
    //"upload_phase":"start",
    //"access_token":"Your_page_access_token"
    //}'
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://graph.facebook.com/v22.0/{$pageId}/video_reels");
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([
        "upload_phase"=>"start",
        "access_token"=>$pageAccessToken
    ]));
    $response = curl_exec($ch);
    $data     = json_decode($response, true);
    if (isset($data['video_id'])) {
        echo "Post successfully created with ID: " . $data['video_id'] . "<br/>";
        echo "Post successfully created with upload_url: " . $data['upload_url'] . "<br/>";
    } else {
        echo "Post created, but no ID returned. Response: " . $response . "<br/>";
        exit;
    }
    curl_close($ch);


    $_init_video_id = $data['video_id'];

//    返回的是这两个参数，
//    $data['video_id']
//    $data['upload_url']这个用来上传视频




    //=============================
    //获取到了视频上传地址，然后上传视频
//    curl -X POST "https://rupload.facebook.com/video-upload/v22.0/video_id" \
//-H "Authorization: OAuth Your_page_access_token" \
//-H "file_url: https://some.cdn.url/video.mp4"
    //=============================
    $videoPath = "https://oa.enpingshi.com/uploadfiles/craft2tiangong/test/01.mp4";

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://rupload.facebook.com/video-upload/v22.0/{$_init_video_id}");
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        "Authorization: OAuth ".$pageAccessToken,
        "file_url : http://oa.enpingshi.com/uploadfiles/craft2tiangong/test/00.mp4"
    ]);

    echo $response = curl_exec($ch);
    $data     = json_decode($response, true);


    print_r($data);
    echo "<br/>";
    echo "<br/>";

    exit;









//    curl -X GET "https://graph.facebook.com/v22.0/video-id
//    ?fields=status
//    &access_token=Your_page_access_token"

    echo $_url="https://graph.facebook.com/v22.0/{$_init_video_id}?fields=status&access_token={$pageAccessToken}";
    echo "<br/>";
    echo "<br/>";

    //获取状态
    $_upload_status = file_get_contents($_url);

    echo $_upload_status;

    echo "<br/>";
    echo "<br/>";


    exit;


    //发布到reels
//    curl -X POST "https://graph.facebook.com/v22.0/page-id/video_reels
//    ?access_token=Your_page_access_token
//    &video_id=video-id
//    &upload_phase=finish
//    &video_state=PUBLISHED
//    &description=What a beautiful day! #sunnyand72"
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://graph.facebook.com/v22.0/{$pageId}/video_reels");
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([
        "access_token" => $pageAccessToken,
        "video_id"     => $_init_video_id,
        "upload_phase" => "finish",
        "video_state"  => "PUBLISHED",
        "description"  => "What a beautiful day! #sunnyand72"
    ]));
    $response = curl_exec($ch);
    $data     = json_decode($response, true);
    if ($data['success'] = true) {
        echo "reels successfully". "<br/>";
    } else {
        echo "reels uploa FAILD". "<br/>";
    }

    curl_close($ch);

    exit;






